package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.constant.dto.BaseFxResponse;
import com.crm.constant.dto.ResultCode;
import com.crm.entity.CrmArticle;
import com.crm.entity.CrmEmployee;
import com.crm.entity.CrmSite;
import com.crm.mapper.CrmArticleMapper;
import com.crm.mapper.CrmSiteMapper;
import com.crm.model.bo.CrmSitePageBo;
import com.crm.model.dto.BasePageResult;
import com.crm.model.vo.ReportCountVo;
import com.crm.service.ICrmSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * CRM-场地表 服务实现类
 * </p>
 *
 * @author CRM_SYSTEM
 * @since 2022-03-30
 */
@Service
public class CrmSiteServiceImpl extends ServiceImpl<CrmSiteMapper, CrmSite> implements ICrmSiteService {


    private static final Integer initCode = 100000;

    @Autowired
    private CrmArticleMapper articleMapper;


    @Override
    public CrmSite queryById(Long siteId) {
        return this.getById(siteId);
    }

    @Override
    public Long saveSite(CrmSite crmSite) {
        Integer siteCode = baseMapper.queryMaxCode();
        if (Objects.isNull(siteCode)) {
            siteCode = initCode;
        } else {
            siteCode += siteCode;
        }
        crmSite.setSiteCode(siteCode);
        this.save(crmSite);
        CrmSite site = this.getOne(new LambdaQueryWrapper<CrmSite>()
                .eq(CrmSite::getSiteCode, siteCode)
                .eq(CrmSite::getIsDeleted, 0));
        return site.getId();
    }

    @Override
    public Long updateSite(CrmSite crmSite) {
        this.updateById(crmSite);
        return crmSite.getId();
    }

    @Override
    public BasePageResult<CrmSite> queryPage(CrmSitePageBo sitePageBo) {

        IPage<CrmSite> pageParam = new Page<>(sitePageBo.getPageNumber(), sitePageBo.getPageSize());
        LambdaQueryWrapper<CrmSite> wrapper = new LambdaQueryWrapper<>();

        if (sitePageBo.getSiteCode() != null) {
            wrapper.like(CrmSite::getSiteCode, sitePageBo.getSiteCode());
        }
        if (StringUtils.isNotBlank(sitePageBo.getSiteName())) {
            wrapper.like(CrmSite::getSiteName, sitePageBo.getSiteName());
        }
        if (StringUtils.isNotBlank(sitePageBo.getSiteAddress())) {
            wrapper.like(CrmSite::getSiteAddress, sitePageBo.getSiteAddress());
        }
        if (sitePageBo.getSiteStatus() != null) {
            wrapper.eq(CrmSite::getSiteStatus, sitePageBo.getSiteStatus());
        }
        if (sitePageBo.getPrice() != null) {
            wrapper.eq(CrmSite::getPrice, sitePageBo.getPrice());
        }
        if (sitePageBo.getIsSports() != null) {
            wrapper.eq(CrmSite::getIsSports, sitePageBo.getIsSports());
        }
        if (sitePageBo.getIsTrain() != null) {
            wrapper.eq(CrmSite::getIsTrain, sitePageBo.getIsTrain());
        }
        wrapper.eq(CrmSite::getIsDeleted, false);
        wrapper.orderByDesc(CrmSite::getGmtCreate);
        IPage<CrmSite> siteIPage = this.page(pageParam, wrapper);
        return BasePageResult.newInstance(siteIPage);
    }

    @Override
    public BaseFxResponse<Boolean> deleteById(Long siteId) {
        return BaseFxResponse.success(this.removeById(siteId));
    }

    @Override
    public List<ReportCountVo> siteMonthCount() {
        return baseMapper.siteMonthCount();
    }
}
